'''
@Author : CaryXiong
@contact : a1816101108@outlook.com
@Date : 2022/03/16 22:10
'''

#-*- coding:utf-8 -*-

from tkinter import *
from tkinter import filedialog
# 正则表达式
import re
# 调用剪切板模块对剪切板进行操作
import pyperclip

import lock
import unlock

class window:
    def __init__(self):
        self.window_main = Tk()
        self.window_main.geometry('400x300')
        self.window_main.resizable(0,0)

    def __about(self):
        self.window_about = Tk()
        self.window_about.geometry('200x100')
        self.window_about.title('关于')
        self.window_about.resizable(0,0)

        self.about_label = Label(self.window_about,text='作者 : Cary_Xiong',font=('等线',12))
        self.about_label.pack()
        self.contact_label = Label(self.window_about,text='联系 : a1816101108@outlook.com',font=('等线',10))
        self.contact_label.pack()

    def __menu(self):
        self.menu = Menu()
        self.menu.add_command(label='关于',font='等线',command=self.__about)
        self.menu.add_command(label='退出',font='等线',command=lambda:exit())
        self.window_main.config(menu=self.menu)
        
    def __frame(self):
        self.frame_left = Frame(width=200,height=300,bd=1,relief='sunken')
        self.frame_right = Frame(width=200,height=300,bd=1,relief='sunken')
        self.frame_left.place(x=0,y=0)
        self.frame_right.place(x=200,y=0)

    def __label(self):
        self.title_left = Label(self.frame_left,text='加密',font=('等线',16))
        self.title_left.place(x=70,y=0)

        self.title_right = Label(self.frame_right,text='解密',font=('等线',16))
        self.title_right.place(x=70,y=0)

        self.entryleft_label = Label(self.frame_left,text='加密字符串:',font=('等线',10))
        self.entryleft_label.place(x=10,y=130)
        self.entryright_label = Label(self.frame_right,text='解密字符串:',font=('等线',10))
        self.entryright_label.place(x=10,y=130)

        self.passwordleft_label = Label(self.frame_left,text='密码(若不填则默认为0*16):',font=('等线',10))
        self.passwordleft_label.place(x=10,y=180)
        self.passwordright_label = Label(self.frame_right,text='密码(若不填则默认为0*16):',font=('等线',10))
        self.passwordright_label.place(x=10,y=180)

        self.showresultleft_label = Label(self.frame_left,font=('等线',10))
        self.showresultleft_label.place(x=10,y=230)
        self.showresultright_label = Label(self.frame_right,font=('等线',10))
        self.showresultright_label.place(x=10,y=230)

    def __radiobutton(self):
        self.var_radiobutton_left = IntVar()
        self.var_radiobutton_left.set(1)

        self.radiobutton_left_1 = Radiobutton(self.frame_left,text='以文件形式上传',font=('等线',10),variable=self.var_radiobutton_left,value=1)
        self.radiobutton_left_2 = Radiobutton(self.frame_left,text='以输入框形式上传',font=('等线',10),variable=self.var_radiobutton_left,value=2)
        self.radiobutton_left_1.place(x=0,y=30)
        self.radiobutton_left_2.place(x=0,y=50)
        
        self.var_radiobutton_right = IntVar()
        self.var_radiobutton_right.set(1)
        
        self.radiobutton_right_1 = Radiobutton(self.frame_right,text='以文件形式上传',font=('等线',10),variable=self.var_radiobutton_right,value=1)
        self.radiobutton_right_2 = Radiobutton(self.frame_right,text='以输入框形式上传',font=('等线',10),variable=self.var_radiobutton_right,value=2)
        self.radiobutton_right_1.place(x=0,y=30)
        self.radiobutton_right_2.place(x=0,y=50)

    def __asklockfile(self):
        '''__file方法内部调用函数'''
        self.lock_path = filedialog.askopenfilename(filetypes=[('txt files','*.txt')])
        print(self.lock_path)
    def __askunlockfile(self):
        '''__file方法内部调用函数'''
        self.unlock_path = filedialog.askopenfilename(filetypes=[('txt files','*.txt')])
        print(self.unlock_path)

    def __file(self):
        self.button_file_left = Button(self.frame_left,text='选择文件',font=('等线',10),command=self.__asklockfile)
        self.button_file_left.place(x=10,y=80)
        self.button_file_right = Button(self.frame_right,text='选择文件',font=('等线',10),command=self.__askunlockfile)
        self.button_file_right.place(x=10,y=80)

    def __entry(self):
        self.entry_left = Entry(self.frame_left,width=20)
        self.entry_left.place(x=10,y=150)
        self.entry_right = Entry(self.frame_right,width=20)
        self.entry_right.place(x=10,y=150)

        self.passwordleft_entry = Entry(self.frame_left,width=20)
        self.passwordleft_entry.place(x=10,y=200)
        self.passwordright_entry = Entry(self.frame_right,width=20)
        self.passwordright_entry.place(x=10,y=200)

    def __enterleft_func(self):
        '''__entry方法内部调用函数'''
        if self.var_radiobutton_left.get() == 1:
            # 以文件形式上传
            print(self.lock_path)
            with open(self.lock_path,encoding='utf-8') as f:
                self.lockfile_content = f.read()
        if self.var_radiobutton_left.get() == 2:
            # 以输入框形式上传
            self.lockstr = self.entry_left.get()
            print(self.lockstr)
        self.lockpassword = self.passwordleft_entry.get()
        check_legal = re.findall('^[0-9a-zA-Z]{16}$',self.lockpassword)
        if self.lockpassword == '':
            self.lockpassword = '0000000000000000'
        elif not check_legal:
            raise ValueError('Illegal password')
        
        if self.var_radiobutton_left.get() == 1:
            self.lockresult = lock.locking(self.lockfile_content,self.lockpassword)
            self.showresultleft_label.config(text=self.lockresult)
        if self.var_radiobutton_left.get() == 2:
            self.lockresult = lock.locking(self.lockstr,self.lockpassword)
            self.showresultleft_label.config(text=self.lockresult)
        
        pyperclip.copy(str(self.lockresult,encoding='utf-8'))
        
    def __enterright_func(self):
        '''__entry方法内部调用函数'''
        if self.var_radiobutton_right.get() == 1:
            # 以文件形式上传
            print(self.unlock_path)
            with open(self.unlock_path,encoding='utf-8') as f:
                self.unlockfile_content = f.read()
        if self.var_radiobutton_right.get() == 2:
            # 以输入框形式上传
            self.unlockstr = self.entry_right.get()
            print(self.unlockstr)
        self.unlockpassword = self.passwordright_entry.get()
        check_legal = re.findall('^[0-9a-zA-Z]{16}$',self.unlockpassword)
        if self.unlockpassword == '':
            self.unlockpassword = '0000000000000000'
        elif not check_legal:
            raise ValueError('Illegal password')

        if self.var_radiobutton_right.get() == 1:
            self.unlockresult = unlock.unlocking(self.unlockfile_content,self.unlockpassword)
            self.showresultright_label.config(text=self.unlockresult)
        if self.var_radiobutton_right.get() == 2:
            self.unlockresult = unlock.unlocking(self.unlockstr,self.unlockpassword)
            self.showresultright_label.config(text=self.unlockresult)
            
        pyperclip.copy(str(self.unlockresult,encoding='utf-8'))

    def __enter(self):
        self.entry_button_left = Button(self.frame_left,text='确定',font=('等线',10),command=self.__enterleft_func)
        self.entry_button_left.place(x=150,y=260)
        self.entry_button_right = Button(self.frame_right,text='确定',font=('等线',10),command=self.__enterright_func)
        self.entry_button_right.place(x=150,y=260)

    def __mainloop__(self):
        self.window_main.mainloop()

    def start(self):
        self.__menu()
        self.__frame()
        self.__label()
        self.__radiobutton()
        self.__file()
        self.__entry()
        self.__enter()
        self.__mainloop__()

if __name__ == '__main__':
    windows = window()
    windows.start()